package com.frame.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;

import com.alibaba.excel.EasyExcel;
import com.frame.config.MinioUpload;
import com.frame.domain.Cargo;
import com.frame.domain.City;
import com.frame.domain.Crop;
import com.frame.domain.Retail;
import com.frame.domain.SrcProcess;
import com.frame.mycommon.Mycommon;
import com.frame.service.CargoService;
import com.frame.service.CityService;
import com.frame.service.CropService;
import com.frame.service.RetailService;
import com.frame.service.SrcProcessService;
import com.frame.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/process")
public class SrcProcessController {

    @Autowired
    SrcProcessService srcProcessService;
    @Autowired
    CityService cityService;
    @Autowired
    CropService cropService;
    @Autowired
    CargoService cargoService;
    @Autowired
    RetailService retailService;
    @Autowired
    KafkaTemplate kafkaTemplate;

//    @Bean
//    public NewTopic newTopic(){
//
//        return new NewTopic("Test5",1,(short) 1);
//
//    }

    /**
     * 加工表查询
     * @param srcProcess
     * @param pageNum
     * @return
     */
    @RequestMapping("/list")
    @Mycommon(value = "列表查询",type = 0)
    public Result list(@RequestBody SrcProcess srcProcess, @RequestParam(defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<SrcProcess> list=srcProcessService.list(srcProcess);
        return Result.success(new PageInfo<>(list));
    }



    @RequestMapping("/getCargo")
    public Result getCatgo()
    {
        List<SrcProcess> list=srcProcessService.gettype();
        return Result.success(list);
    }
    /**
     * 加工表添加数据
     * @param srcProcess
     * @return
     */
    @RequestMapping("/add")
    @Mycommon(value = "列表添加",type = 1)
    public Result add(@RequestBody SrcProcess srcProcess){
        srcProcess.setCreateTime(new Date());
        boolean b=srcProcessService.add(srcProcess);
//        kafkaTemplate.send("Test5","kafka消息")
//                .addCallback(new ListenableFutureCallback() {
//                    @Override
//                    public void onFailure(Throwable throwable) {
//                        System.out.println("发送消息异常");
//                    }
//
//                    @Override
//                    public void onSuccess(Object o) {
//                        System.out.println("发送消息成功");
//                    }
//                });
        return b?Result.success("添加成功"):Result.fail("添加失败");
    }

    /**
     * 加工表修改数据
     * @param srcProcess
     * @return
     */
    @RequestMapping("/update")
    @Mycommon(value = "列表修改",type = 2)
    public Result update(@RequestBody SrcProcess srcProcess){
        boolean b=srcProcessService.update(srcProcess);
        return b?Result.success("修改成功"):Result.fail("修改失败");
    }

    /**
     * 加工表删除数据
     * @param processId
     * @return
     */
    @RequestMapping("/del")
    @Mycommon(value = "列表删除",type = 3)
    public Result del(Integer processId){
        boolean b=srcProcessService.del(processId);
        return b?Result.success("删除成功"):Result.fail("删除失败");
    }


    @RequestMapping("/gettype")
    public Result gettype(){

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        List<City> nodeList = cityService.getlist();

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<Object>> treeNodes = TreeUtil.<City, Object>build(nodeList, 1, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getCityId());
                    tree.setParentId(treeNode.getPid());
                    tree.setName(treeNode.getCityname());

                });

        return Result.success(treeNodes);

    }

    /**
     * 查询农作物  下拉框使用
     * @return
     */
    @RequestMapping("/getCropList")
    public Result getCropList(){
        List<Crop> list=cropService.getProcessList();
        return Result.success(list);
    }

    /**
     * 查询仓库  下拉框使用
     * @return
     */
    @RequestMapping("/getCargoList")
    public Result getCargoList(){
        List<Cargo> list=cargoService.getProcessList();
        return Result.success(list);
    }

    /**
     * 查询零售商  下拉框使用
     * @return
     */
    @RequestMapping("/getRetailList")
    public Result getRetailList(){
        List<Retail> list=retailService.getProcessList();
        return Result.success(list);
    }

    /**
     * 加工表导出excel
     * @param response
     * @throws IOException
     */
    @RequestMapping("/export")
    public void exportPostExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("加工信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<SrcProcess> list=srcProcessService.list2();

        EasyExcel.write(response.getOutputStream(),SrcProcess.class).sheet("加工信息").doWrite(list);

    }

    /**
     * 添加图片
     * @param file
     * @return
     */
    @RequestMapping("/addPic")
    public String addPic(MultipartFile file){
        String s = MinioUpload.uploadPic(file);
        return s;
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @RequestMapping("/search")
    public Result search(Integer id){
        SrcProcess srcProcess=srcProcessService.search(id);
        return Result.success(srcProcess);
    }

    /**
     * 报表 折线图
     * @return
     */
    @RequestMapping("/getTu")
    public Result getTu(){
        List<Map<String,String>> list=srcProcessService.gettu();
        return Result.success(list);
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @RequestMapping("/import")
    public Map<String,Object> getImport(MultipartFile file){
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(),SrcProcess.class,new CellDataDemoHeadDataListenerProcess(srcProcessService)).sheet().doRead();
            hashMap.put("msg","成功");
        } catch (IOException e) {
            e.printStackTrace();
            hashMap.put("msg","失败");
        }
        return hashMap;
    }

    @RequestMapping("/getbaobiao")
    public Result getbaobiao(){
        List<Map<String,String>> list = srcProcessService.getlist3();
        return Result.success(list);
    }


//    public Result Email(){
//        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//        simpleMailMessage.setFrom("1399426606@qq.com");
//        simpleMailMessage.setTo("2682707696@qq.com");
//        simpleMailMessage.setSubject("嘻嘻哈哈哈");
//        simpleMailMessage.setText("尊敬的用户，祝您生日快乐！");
//        javaMailSender.send(simpleMailMessage);
//        return Result.success();
//    }




//    public void kafka(){
//        kafkaTemplate.send("send_08_27","kafka消息")
//                .addCallback(new ListenableFutureCallback() {
//                    @Override
//                    public void onFailure(Throwable throwable) {
//                        System.out.println("发送消息异常");
//                    }
//
//                    @Override
//                    public void onSuccess(Object o) {
//                        System.out.println("发送消息成功");
//                    }
//                });
//    }

    @RequestMapping("/qs")
    public Result yunshu(Integer processId){
         List<SrcProcess> list=srcProcessService.yunshu(processId);
        return Result.success(list);
    }


}
